import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

public class Solution {

     public static class TreeNode {
         int val = 0;
         TreeNode left = null;
         TreeNode right = null;

         public TreeNode(int val) {
         this.val = val;

         }
     }

    public static ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        Queue<TreeNode> q = new ArrayDeque<>();
        TreeNode node;

        if(root!=null){
            q.add(root);
        }

        while(q.peek()!=null){
            node = q.poll();
            result.add(node.val);
            if(node.left!=null)
                q.add(node.left);
            if(node.right!=null)
                q.add(node.right);
        }
        return result;
    }
    public static void main(String args[]){
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(5);
        PrintFromTopToBottom(root);

//        Solution s1 = new Solution();

    }
}